package com.zlop.abc;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class Printer implements Runnable { 
	
	private static final int PRINT_COUNT = 10;
	
	private ReentrantLock reentrantLock;
	
	private Condition thisCondition;
	
	private Condition nextCondition;
	
	private char printChar;
	
	public Printer(ReentrantLock reentrantLock, Condition thisCondition, Condition nextCondition, char printChar){
		this.reentrantLock = reentrantLock;
		this.thisCondition = thisCondition;
		this.nextCondition = nextCondition;
		this.printChar = printChar;
	}

	@Override
	public void run() {
		reentrantLock.lock();
		try{
			for(int i=0; i<PRINT_COUNT; i++){
				System.out.print(printChar);
				nextCondition.signal();
				if(i < PRINT_COUNT - 1){
					try {
						thisCondition.await();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}finally{
			reentrantLock.unlock();
		}
	}

}
